/**
 * common use tools
 */
var Helper={
    getTr: function(el){
        return this.getParentElement(el, 'tr');
    },
    getParentElement: function(el, tag){
        while(el.get('tag')!=tag){
            el=el.getParent();
        }
        
        return el;
    },
    log: function(obj){
        if(console){
            console.log(obj);
        }
    },
    putAjaxLoader: function(container){
        container.set('html','');
        new Element('div', {'class':'ajax-loader-wrap'}).grab(
            new Element('img', {'src':BASE_URL+'/media/img/ajax-loader.gif', 'alt':'loading...', 'class':'ajax-loader'})
        ).inject(container);
    },
    windowOpen: function(url, name, options){
        var opts = '';
        var spc = '';
        for(var i in options){
            if(opts != ''){
                spc = ',';
            }
            opts = opts+spc+i+'='+options[i];
        }
        if(opts == ''){
            opts = 'toolbar=0,menubar=0,location=0,resizable=1,width=950,height=400';
        }
        
/*
status	The status bar at the bottom of the window.
toolbar	The standard browser toolbar, with buttons such as Back and Forward.
location	The Location entry field where you enter the URL.
menubar	The menu bar of the window
directories	The standard browser directory buttons, such as What’s New and What’s Cool
resizable	Allow/Disallow the user to resize the window.
scrollbars	Enable the scrollbars if the document is bigger than the window
height	Specifies the height of the window in pixels. (example: height=’350′)
width	Specifies the width of the window in pixels.
 */
        
//        window.open ("http://www.javascript-coder.com","mywindow","menubar=1,resizable=1,width=350,height=250");
        window.open(url,name,opts);
    }
    
};

var Tabs = new Class({
    Implements: Options,
    options: {
        
    },
    initialize: function(options) {
        this.setOptions(options);
    },
    initTabs: function(){
        var self = this;
        
        try{
            $(document.body).getElements('.tabs-wrap').each(function(tab_wrap){
                self.initTab(tab_wrap);
            })
        }catch(e){
            Helper.log(e);
        }
    },
    closeBodyTabs: function(bodyTabs){
        var self = this;
        var i=0;
        bodyTabs.each(function(bodyTab){
           bodyTab.setStyle('display', 'none');
           bodyTab.store('number', i);
        });
    },
    closeHeadTabs: function(headTabs){
        var self = this;
        var i=0;
        headTabs.each(function(headTab){
           headTab.addClass('unactive-tab-head');
           headTab.removeClass('active-tab-head');
           headTab.store('number', i);
           i++;
        });
    },
    openHeadTab: function(headTab){
        headTab.removeClass('unactive-tab-head');
        headTab.addClass('active-tab-head');
    },
    openBodyTab: function(bodyTab){
        bodyTab.setStyle('display', 'block');
    },
    closeBodyTab: function(bodyTab){
        bodyTab.setStyle('display', 'none');
    },
    initTab: function(tab_wrap){
        var self = this;
        
        var headTabs = tab_wrap.getElements('.tab-head');
        var bodyTabs = tab_wrap.getElements('.tab-body');
        
        self.closeHeadTabs(headTabs);
        self.closeBodyTabs(bodyTabs);
        self.openBodyTab(bodyTabs[0]);
        self.openHeadTab(headTabs[0]);
        
        headTabs.each(function(headTab){
           headTab.addEvent('click', function(){
               var number = headTab.retrieve('number');
               self.closeBodyTabs(bodyTabs);
               self.closeHeadTabs(headTabs);
               self.openBodyTab(bodyTabs[number]);
               self.openHeadTab(headTabs[number]);
           });
        });
    }
});


var OrderTool = new Class({
    Implements: Options,
    
    options: {
        form: null,
        onComplete: null
        
    },

    initialize: function(options) {
        this.setOptions(options);
        
    },

    order_by: function(order_by, order_how){
        if(this.options.form){
            $(this.options.form).getElement('input[name=order_by]') = order_by;
            $(this.options.form).getElement('input[name=order_how]') = order_how;

            if(this.options.onComplete){
                this.options.onComplete();
            }
        }
    }
});


var AlertInfo = new Class({
    Implements: Options,
    options: {
        text: 'wczytywanie...',
        div_class: 'neutral-alert',
        width: 350,
        height: 120,
        opacity: 0.8,
        error_mess: 'Błąd wczytywania',
        showTime: 1000
    },
    div: null,
    initialize: function(options) {
        this.setOptions(options);
        
        this.div = new Element('div', {'class':'alert-info '+this.options.div_class})
        new Element('div', {'class':'alert-info-message', 'html':this.options.text}).inject(this.div);
        this.div.setStyle('display','none');
        this.div.set('opacity', '0');
        
        this.div.store('AIObject', this);
    },
    show: function(time){
        if(!time){
            time=this.options.showTime;
        }
        
        var div = this.div;
        var self = this;
        $(window.document.body).grab(div);
        
        div.addEvent('hide', function(){
           self.hide(); 
        });
        
        this.fx = new Fx.Morph(div,{
            duration: time,
            transition: Fx.Transitions.Expo.easeIn,
            onStart: function(){
                
                div.setStyle('display', 'block');
            }
        });
        
        if(time){
            self.fx.addEvent('complete', function(){
                
            })
        }
        
        this.fx.start({
            opacity: [0, self.options.opacity]
        });
        
    },
    hide: function(){
        var div = this.div;
        var self = this;
        
        if(this.fx){
            this.fx.cancel();
        }
        
        this.fx = new Fx.Morph(div, {
            onComlete: function(){
                self.destroy();
            }
        });
        
        this.fx.start({
            height: 0,
            width: 0,
            top: 0,
            left: 0,
            opacity: 0
        });
    },
    
    error: function(mess){
        var div = this.div;
        var self = this;
        
        if(!mess){
            mess=this.options.error_mess;
        }
        
        div.addClass('alert-info-error');
        var info = div.getElement('.alert-info-message');
        info.set('html', mess);
        
        div.highlight('#fff');
        
        div.addEvent('click', function(event){
            self.hide();
        })
        
    },
    
    destroy: function(){
        var div = this.div;
        var self = this;
        
        if(this.fx){
            this.fx.cancel();
        }
        
        div.destroy();
        
        delete(this);
    }
    
});


window.addEvent('domready', function(){

});